package com.yang.dao;

import com.yang.entity.ProductCategory;

import java.sql.*;
import java.util.*;

public class ProductCategoryDao {
    //查询全部
    public List<ProductCategory> queryProductCategory() {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from ProductCategory");
            rs = ps.executeQuery();
            List<ProductCategory> list = new ArrayList<>();
            while (rs.next()) {
                ProductCategory obj = new ProductCategory();
                obj.setId(rs.getInt("id"));
                obj.setName(rs.getString("name"));
                obj.setParentId(rs.getInt("parentId"));
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //单个查询
    public ProductCategory queryProductCategoryById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from ProductCategory where id=?");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                ProductCategory obj = new ProductCategory();
                obj.setId(rs.getInt("id"));
                obj.setName(rs.getString("name"));
                obj.setParentId(rs.getInt("parentId"));
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //添加
    public int saveProductCategory(ProductCategory obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("insert into product_category(name, parent_id) values (?, ?)");
            ps.setString(1, obj.getName());
            ps.setInt(2, obj.getParentId());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //删除
    public int deleteProductCategoryById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("delete from ProductCategory where id=?");
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //修改
    public int updateProductCategoryById(ProductCategory obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("update product_category set name=?, parent_id=? where id=?");
            ps.setString(1, obj.getName());
            ps.setInt(2, obj.getParentId());
            ps.setInt(3, obj.getId());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }
}
